$(document).ready(function() {
	var __ArtiForm = $('#form_article_add');
	var __editForm = $('#form_authority_edit');

	$('#submit_form').click(function() {
		var objArtiForm = {
			__formName: $("input[name='name']", __ArtiForm),
			__formRemark: $("textarea[name='remark']", __ArtiForm)
		}
		for (var i in objArtiForm) {

			if (objArtiForm[i].val() == '') {
				objArtiForm[i].css({
					'border-color': 'red'
				});
				return false;
			} else {
				objArtiForm[i].css({
					'border-color': '#e5e5e5'
				});
			}

		}


		$.ajax({
			type: "POST",
			url: "/Typical/add",
			data: {
				"name": objArtiForm.__formName.val(),
				"remark": objArtiForm.__formRemark.val()
			},
			success: function(data) {
				layer.alert(data.info, function() {
					if (true === data.status) window.location = "/Typical/list";
				  
				});

			}
		});

	});

	//$('.tableDelete').click(function(){
	$(document).on('click', '.tableDelete', function() {
		var userId = $(this).parent('td').attr('item_id'),
			thisUrl = $(this).attr('href');
		//官网欢迎页   
		layer.confirm('确认删除', {
			btn: ['确认', '取消'] //按钮
		}, function(index) {

			$.ajax({
				type: "GET",
				url: thisUrl,
				async: true,
				data: {
					//"id" : userId
				},
				success: function(data) {
					layer.alert(data.info, function() {
						if (true === data.status) window.location = "/Typical/list";
					});
				}
			});
			layer.close(index);
			return false;

		}, function(index) {
			layer.close(index);
		});
		return false;

	});



	//$('.tableEdit').click(function(e){
	$(document).on('click', '.tableEdit', function(e) {
		var urlOld = $(this).attr('href'),
			urlArr = urlOld.split("/");
		urlId = urlArr[urlArr.length - 1];
		//$(this).attr({'href':'#form_modal_edit','data-toggle':'modal','data-target':'#form_modal_edit'});
		$('#form_modal_edit').modal();

		var __parent = $(this).parent('td').parent('tr.gradeX'),
			__formName = $("input[name='name']", __editForm),

			__formRemark = $("textarea[name='remark']", __editForm);

		var tdTable = [];
		$('td', __parent).each(function() {
			tdTable.push($(this).text());
		});
		__formName.val(tdTable[0]);

		__formRemark.val(tdTable[1]);
		return false;
	});
	$('#edit_form').click(function() {

		var objArtiForm = {
			__formName: $("input[name='name']", __editForm),

			__formRemark: $("textarea[name='remark']", __editForm)
		}
		for (var i in objArtiForm) {

			if (objArtiForm[i].val() == '') {
				objArtiForm[i].css({
					'border-color': 'red'
				});
				return false;
			} else {
				objArtiForm[i].css({
					'border-color': '#e5e5e5'
				});
			}

		}


		$.ajax({
			type: "POST",
			url: "/Typical/edit/id/" + urlId,
			data: {
				"name": objArtiForm.__formName.val(),

				"remark": objArtiForm.__formRemark.val()
			},
			success: function(data) {
				layer.alert(data.info, function() {
					if (true === data.status) window.location = "/Typical/list";
				});
			}
		});

	});

	$(document).on('click', '#page_next,#page_prev,#page_index,#page_end', function() {
		var urlOld = $(this).attr('href'),
			urlArr = urlOld.split("/");
		urlId = urlArr[urlArr.length - 1];
		$.ajax({
			type: 'POST',
			url: '/Typical/page/page/' + urlId,
			data: urlId,
			success: function(data) {
				var data = $.parseJSON(data);

				$('#tbody').html('');
				var id = '';

				$.each(data, function(k, v) {
					//console.log('k='+k);
					id = v['article_type_id'];
					//alert(id);
					$('#tbody').append($('<tr class="gradeX"><td>' + v['name'] + '</td><td>' + v['remark'] + '</td><td item_id="' + v['sites_id'] + '" cate_id="' + id + '"><a class="tableDelete" href="/Typical/del/id/' + id + '">删除</a> / <a class="tableEdit" href="/Typical/edit/id/' + id + '">修改</a> </td></tr>'));



				});

			}

		});
		$.ajax({
			type: 'POST',
			url: '/Typical/currentPage/page/' + urlId,
			data: urlId,
			success: function(data) {
				//console.log(data);
				$('#pages').html(data);
				$('#pages').ready(function() {
					nextBtn = $('#page_next');
				});
			}

		});
		return false;
	});

});